.TH std::variant::index 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::variant::index \- std::variant::index

.SH Synopsis
   constexpr std::size_t index() const noexcept;  \fI(since C++17)\fP

   Returns the zero-based index of the alternative that is currently held by the
   variant.

   If the variant is valueless_by_exception, returns variant_npos.

.SH Example


// Run this code

 #include <variant>
 #include <string>
 #include <iostream>
 int main()
 {
     std::variant<int, std::string> v = "abc";

     std::cout << "v.index = " << v.index() << '\\n';

     v = {};

     std::cout << "v.index = " << v.index() << '\\n';
 }

.SH Output:

 v.index = 1
 v.index = 0

.SH See also

   holds_alternative checks if a variant currently holds a given type
   \fI(C++17)\fP           \fI(function template)\fP
   get(std::variant) reads the value of the variant given the index or the type (if the
   \fI(C++17)\fP           type is unique), throws on error
                     \fI(function template)\fP
